//
//  OffLineTableViewCell.m
//  xpkc
//
//  Created by TopSageOSX on 14-8-25.
//  Copyright (c) 2014年 wanmeizty. All rights reserved.
//

#import "OffLineTableViewCell.h"

@implementation OffLineTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier tag:(NSInteger)tag{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        if (tag == 0) {
            [self creatDownloadedCellView];
        }else{
            [self creatDownloadingCellView];
        }
    }
    return self;
}
- (void)awakeFromNib
{
    // Initialization code
}
/**
 *  创建下载中cell的视图
 *  有课程没有章节
 *  有进度标签没有大小标签
 *  高：100
 */
- (void)creatDownloadingCellView{
    UILabel *myLine = [[UILabel alloc]initWithFrame:CGRectMake(isPad?25:23, 89, SCREEN_WIDTH, 1)];
    myLine.backgroundColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1];
    [self addSubview:myLine];
    /**
     创建控件
     */
    self.titleLabel = [[UILabel alloc]init];
    self.lectureLabel = [[UILabel alloc]init];
    self.teacherLabel = [[UILabel alloc]init];
    self.playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.progress = [[UIProgressView alloc]init];
    self.progressLabel = [[UILabel alloc]init];
    self.durationLabel = [[UILabel alloc]init];
    self.statusLabel = [[UILabel alloc]init];
    self.bgProgress = [[UIView alloc]init];
    //固定的标签
    UILabel *lecturetext = [[UILabel alloc]init];
    UILabel *teachertext = [[UILabel alloc]init];
    UILabel *durationtext = [[UILabel alloc]init];
    /**
     *  设定位置
     */
    CGRect title_frame;
    CGRect lecturetext_frame,lecture_frame;
    CGRect teachertext_frame,teacher_frame;
    CGRect durationtext_frame,duration_frame;
    CGRect play_btn_frame;
    CGRect progress_frame,progress_label_frame;
    CGRect status_frame;
    if (isPad) {
        //
        title_frame = RECT(20, 0, SCREEN_WIDTH-10-60, 40);
        lecturetext_frame = RECT(20, 40, 50, 20);
        lecture_frame = RECT(55, 40, SCREEN_WIDTH-60-10-40, 20);
        teachertext_frame = RECT(20, 60, 50, 20);
        teacher_frame = RECT(55, 60, 100, 20);
        durationtext_frame = RECT(SCREEN_WIDTH/2+10, 60, 50, 20);
        duration_frame = RECT(SCREEN_WIDTH/2+45, 60, 50, 20);
        play_btn_frame = RECT(SCREEN_WIDTH-60, 10, 60, 60);
        status_frame = RECT(SCREEN_WIDTH-45, 53, 60, 20);
        progress_frame = RECT(25, 80, SCREEN_WIDTH-50-60+10, 5);
        progress_label_frame = RECT(SCREEN_WIDTH-60-30, 76, 90, 10);
    }else{
        title_frame = RECT(20, 0, SCREEN_WIDTH-10-60, 40);
        lecturetext_frame = RECT(20, 40, 50, 20);
        lecture_frame = RECT(55, 40, SCREEN_WIDTH-60-10-40, 20);
        teachertext_frame = RECT(20, 60, 50, 20);
        teacher_frame = RECT(55, 60, 100, 20);
        durationtext_frame = RECT(SCREEN_WIDTH/2+10, 60, 50, 20);
        duration_frame = RECT(SCREEN_WIDTH/2+45, 60, 50, 20);
        play_btn_frame = RECT(SCREEN_WIDTH-60, 10, 60, 60);
        status_frame = RECT(SCREEN_WIDTH-45, 53, 60, 20);
        progress_frame = RECT(25, 85, SCREEN_WIDTH-10-60-30+10, 5);
        progress_label_frame = RECT(SCREEN_WIDTH-60-25-5, 75, 90, 10);
    }
    self.titleLabel.frame = title_frame;
    self.teacherLabel.frame = teacher_frame;
    teachertext.frame = teachertext_frame;
    self.lectureLabel.frame = lecture_frame;
    lecturetext.frame = lecturetext_frame;
    self.durationLabel.frame = duration_frame;
    durationtext.frame = durationtext_frame;
    self.playBtn.frame = play_btn_frame;
    self.statusLabel.frame = status_frame;
    self.progress.frame = progress_frame;
    self.progressLabel.frame = progress_label_frame;
    /**
     *  详细设定
     */
    self.titleLabel.font = FONT(15);
    self.lectureLabel.font = FONT_SYS_LITTLE;
    self.teacherLabel.font = FONT_SYS_LITTLE;
    self.durationLabel.font = FONT_SYS_LITTLE;
    self.statusLabel.font = FONT(10);
    lecturetext.font = FONT_SYS_LITTLE;
    teachertext.font = FONT_SYS_LITTLE;
    durationtext.font = FONT_SYS_LITTLE;
    
    lecturetext.text = @"课程:";
    teachertext.text = @"讲师:";
    durationtext.text = @"时长:";
    lecturetext.textColor = [UIColor darkGrayColor];
    teachertext.textColor = [UIColor darkGrayColor];
    durationtext.textColor = [UIColor darkGrayColor];
    self.bgProgress.backgroundColor = [UIColor colorWithRed:0.3 green:0.95 blue:0.6 alpha:0.1];
    self.teacherLabel.textColor = [UIColor darkGrayColor];
    self.lectureLabel.textColor = [UIColor darkGrayColor];
    self.durationLabel.textColor = [UIColor darkGrayColor];
    self.statusLabel.textColor = [UIColor darkGrayColor];
    self.progress.progressTintColor = GreenColor;
    self.progressLabel.font = FONT_SYS(10);
    self.progressLabel.textAlignment = NSTextAlignmentRight;
    /**
     *  添加视图
     */
    [self.contentView addSubview:self.bgProgress];
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:lecturetext];
    [self.contentView addSubview:self.lectureLabel];
    [self.contentView addSubview:teachertext];
    [self.contentView addSubview:self.teacherLabel];
    [self.contentView addSubview:durationtext];
    [self.contentView addSubview:self.durationLabel];
    [self.contentView addSubview:self.playBtn];
    [self.contentView addSubview:self.statusLabel];
//    [self.contentView addSubview:self.progress];
    [self.contentView addSubview:self.progressLabel];
}
/**
 *  创建下载完成cell
 *  有章节没课程
 *  有大小，没进度标签和进度条
 *  高：80
 */
- (void)creatDownloadedCellView{
    UILabel *myLine = [[UILabel alloc]initWithFrame:CGRectMake(isPad?25:23, 79, SCREEN_WIDTH, 1)];
    myLine.backgroundColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1];
    [self addSubview:myLine];
    UIView *pointView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 60)];
    if (isPad) {
        self.line = [[UILabel alloc]initWithFrame:CGRectMake(19.5, 0, 1, 100)];
    }else{
        self.line = [[UILabel alloc]initWithFrame:CGRectMake(19.5, 0, 1, 80)];
    }
    self.line.layer.borderWidth = 1;
    self.line.layer.borderColor = GreenColor.CGColor;
    [pointView addSubview:self.line];
    if (isPad) {
        _point = [[UILabel alloc]initWithFrame:CGRectMake(10, 15, 20, 11)];
        _point.layer.cornerRadius = 10;
        //    point.backgroundColor = [UIColor blueColor];
        _point.text = @"●";
        _point.font = [UIFont systemFontOfSize:24];
        _point.textAlignment = NSTextAlignmentCenter;
        _point.textColor = GreenColor;
        [pointView addSubview:_point];
    }else{
        _point = [[UILabel alloc]initWithFrame:CGRectMake(10, 15, 20, 11)];
        _point.layer.cornerRadius = 10;
        //    point.backgroundColor = [UIColor blueColor];
        _point.text = @"●";
        _point.font = [UIFont systemFontOfSize:24];
        _point.textAlignment = NSTextAlignmentCenter;
        _point.textColor = GreenColor;
        [pointView addSubview:_point];
    }

    /**
     创建控件
     */
    self.titleLabel = [[UILabel alloc]init];
    self.chapterLabel = [[UILabel alloc]init];
    self.teacherLabel = [[UILabel alloc]init];
    self.durationLabel = [[UILabel alloc]init];
    self.sizeLabel = [[UILabel alloc]init];
    self.playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.jurisdiction = [[UILabel alloc]init];
    //固定的标签
    UILabel *chaptertext = [[UILabel alloc]init];
    UILabel *teachertext = [[UILabel alloc]init];
    UILabel *durationtext = [[UILabel alloc]init];
    UILabel *sizetext = [[UILabel alloc]init];
    /**
     *  设定位置
     */
    CGRect title_frame;
    CGRect chaptertext_frame,chapter_frame;
    CGRect teachertext_frame,teacher_frame;
    CGRect durationtext_frame,duration_frame;
    CGRect sizetext_frame,size_frame;
    CGRect play_btn_frame;
    CGRect jurisdiction_frame;
    if (isPad) {
        //
        title_frame = RECT(40, 0, SCREEN_WIDTH-10-60, 40);
        chaptertext_frame = RECT(40, 40, 50, 20);
        chapter_frame = RECT(100, 40, SCREEN_WIDTH-10-10-60, 20);
        teachertext_frame = RECT(40, 60, 50, 20);
        teacher_frame = RECT(100, 60, 100, 20);
        durationtext_frame = RECT(230, 60, 50, 20);
        duration_frame = RECT(290, 60, 100, 20);
        sizetext_frame = RECT(430, 60, 40, 20);
        size_frame = RECT(430, 60, SCREEN_WIDTH-200, 20);
        play_btn_frame = RECT(SCREEN_WIDTH-60, 0, 80, 80);
        jurisdiction_frame = RECT(100, 10, 100, 60);
    }else{
        title_frame = RECT(40, 0, SCREEN_WIDTH-10-60, 40);
        chaptertext_frame = RECT(40, 35, 40, 20);
        chapter_frame = RECT(80, 35, SCREEN_WIDTH-10-10-60, 20);
        teachertext_frame = RECT(40, 55, 40, 20);
        teacher_frame = RECT(80, 55, 70, 20);
        durationtext_frame = RECT(190, 55, 50, 20);
        duration_frame = RECT(230, 55, 100, 20);
        sizetext_frame = RECT(230, 55, 40, 20);
        size_frame = RECT(270, 55, SCREEN_WIDTH-240-10, 20);
        play_btn_frame = RECT(SCREEN_WIDTH-60, 10, 80, 80);
        jurisdiction_frame = RECT(100, 10, 100, 60);
    }
    self.titleLabel.frame = title_frame;
    self.teacherLabel.frame = teacher_frame;
    teachertext.frame = teachertext_frame;
    self.chapterLabel.frame = chapter_frame;
    chaptertext.frame = chaptertext_frame;
    self.playBtn.frame = play_btn_frame;
    self.durationLabel.frame = duration_frame;
    durationtext.frame = durationtext_frame;
    sizetext.frame = sizetext_frame;
    self.sizeLabel.frame = size_frame;
    self.jurisdiction.frame = jurisdiction_frame;
    /**
     *  详细设定
     */
    self.titleLabel.font = FONT_SYS_LITTLE;
    self.teacherLabel.font = FONT_SYS_LITTLE;
    self.chapterLabel.font = FONT_SYS_LITTLE;
    self.durationLabel.font = FONT_SYS_LITTLE;
    chaptertext.font = FONT_SYS_LITTLE;
    teachertext.font = FONT_SYS_LITTLE;
    durationtext.font = FONT_SYS_LITTLE;
    
    teachertext.text = @"讲师:";
    chaptertext.text = @"章节:";
    durationtext.text = @"时长:";
    sizetext.text = @"大小:";
    teachertext.textColor = [UIColor darkGrayColor];
    chaptertext.textColor = [UIColor darkGrayColor];
    durationtext.textColor = [UIColor darkGrayColor];
    sizetext.textColor = [UIColor darkGrayColor];
    self.chapterLabel.textColor = [UIColor darkGrayColor];
    self.teacherLabel.textColor = [UIColor darkGrayColor];
    self.sizeLabel.textColor = [UIColor darkGrayColor];
    self.durationLabel.textColor = [UIColor darkGrayColor];
    
    self.jurisdiction.textColor = [UIColor redColor];
    self.jurisdiction.text = @"账号权限";
    self.jurisdiction.layer.borderWidth = 1;
    self.jurisdiction.layer.borderColor = [UIColor redColor].CGColor;
    self.jurisdiction.hidden = YES;
    /**
     *  添加视图
     */
    [self.contentView addSubview:self.bgProgress];
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:chaptertext];
    [self.contentView addSubview:self.chapterLabel];
    [self.contentView addSubview:teachertext];
    [self.contentView addSubview:self.teacherLabel];
    [self.contentView addSubview:durationtext];
    [self.contentView addSubview:self.durationLabel];
//    [self.contentView addSubview:sizetext];
//    [self.contentView addSubview:self.sizeLabel];
    [self.contentView addSubview:self.playBtn];
    [self.contentView addSubview:pointView];
    [self.contentView addSubview:self.jurisdiction];
}
/**
 *  更新下载进度
 *
 *  @param item 下载实例
 */
-(void)updateCellProgressWithItem:(DownloadItem *)item{
    self.bgProgress.frame = CGRectMake(0, 1, SCREEN_WIDTH*item.videoDownloadProgress, 87);
//    self.progress.progress = item.videoDownloadProgress;
    float downloadSize = item.videoDownloadedSize/1024.0/1024.0;
    float filetotalSize = item.videoTotalFileSize/1024.0/1024.0;
    self.progressLabel.text = [NSString stringWithFormat:@"%0.1fM/%0.1fM",downloadSize,filetotalSize];
}
/**
 *  更新下载状态按钮
 *
 *  @param downloadStatu 下载状态
 */
-(void)updateCellDownloadStatuesWithStatus:(DownloadStatu)downloadStatu{
    UIImage *image;
    if (downloadStatu) {
        switch (downloadStatu) {
            case DownloadStatusWait:
                image = [UIImage imageNamed:@"download-stat-waiting"];
                break;
            case DownloadStatusStart:
                image = [UIImage imageNamed:@"download-status-down"];
                break;
            case DownloadStatusPause:
                image = [UIImage imageNamed:@"download-status-hold"];
                break;
            case DownloadStatusFinish:
                image = [UIImage imageNamed:@"download-play-button"];
                break;
            case DownloadStatusFail:
                image = [UIImage imageNamed:@"download-status-fail"];
                break;
            case DownloadStatusDownloading:
                image = [UIImage imageNamed:@"download-status-down"];
                break;
                
            default:
                break;
        }
        if (IsIOS7) {
            image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        }
        [self.playBtn setImage:image forState:UIControlStateNormal];
    }else{
        //默认的图片
        image = [UIImage imageNamed:@"download-stat-waiting"];
        if (IsIOS7) {
            image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        }
        [self.playBtn setImage:image forState:UIControlStateNormal];
    }
    
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
